<script>
    $(function() {
    	$('#sensorForm tbody tr').formset({
        	prefix: '{{ sensorFS.prefix }}'
        });
    })
   	function submitForm() {
		$.ajax({
			type: "POST",
			url: '/pmp/sensors/',
			data: $('form#sensorForm').serialize(),
			datatype: "json",
			success: function(data){
				//console.log(data);
				$('#sensorForm').html(data);
				$('#success').fadeIn().delay(2000).fadeOut(1000);        
			}
		});
	}
	$.fn.serializeObject = function() {
	  var arrayData, objectData;
	  arrayData = this.serializeArray();
	  objectData = {};
	
	  $.each(arrayData, function() {
	    var value;
	
	    if (this.value != null) {
	      value = this.value;
	    } else {
	      value = '';
	    }
	
	    if (objectData[this.name] != null) {
	      if (!objectData[this.name].push) {
	        objectData[this.name] = [objectData[this.name]];
	      }
	
	      objectData[this.name].push(value);
	    } else {
	      objectData[this.name] = value;
	    }
	  });
	
	  return objectData;
	};
</script>
{% load widget_tweaks %}
{% if show %}
	<form id="sensorForm" method="post" name="sensorForm" action="">
		{{ sensorFS.management_form }}
		<table border="0" cellpadding="0" cellspacing="0">
			<tbody>	
				<tr class="spaceUnder">
					<td>Name</td>
					<td>Device</td>
					<td>Path</td>
					<td>Type</td>
					<td>Category</td>
					<td>Height</td>
					<td>Description</td>
					<td>UUID</td>
				</tr>
	        	{% for form in sensorFS %}
	        	{{ form.id }}
	        	<input type="hidden" name="projectId" value="{{ projectKey }}" \>
	        		<tr>
	        			<td>{{ form.name|add_class:"form-inline span2" }}{{ form.name.errors }}</td>
	        			<td>{{ form.device|add_class:"form-inline span2" }}{{ form.device.errors }}</td>
	        			<td>{{ form.path|add_class:"form-inline span2" }}{{ form.path.errors }}</td>
	    				<td>{{ form.sensor_type|add_class:"form-inline span2" }}{{ form.sensor_type.errors }}</td>
	    				<td>{{ form.pmp_category|add_class:"form-inline span2" }}{{ form.pmp_category.errors }}</td>
	    				<td>{{ form.sensor_height|add_class:"form-inline span1" }}{{ form.sensor_height.errors }}</td>
	    				<td>{{ form.description|add_class:"form-inline span2" }}{{ form.grid.errors }}</td>
	    				<td>{{ form.uuid|add_class:"form-inline span1" }}{{ form.uuid.errors }}</td>
	    				<td>{% if form.instance.pk %}{{ form.DELETE }}{% endif %}</td>
	    			</tr>
	    		{% endfor %}
			</tbody>
		</table><br>
	    <p><input class='btn' type="submit" value="Save" onclick="submitForm(); return false;"/>
	    	<span id="success"><strong>&nbsp;&nbsp;{{ saveStatus }}</strong></span></p>
	</form>
{% endif %}
